package mv.releaser.model;

public class StringUtilities {

	public static String capitalize(String str) {
		StringBuffer s = new StringBuffer();

		char ch; // One of the characters in str.
		char prevCh; // The character that comes before ch in the string.
		int i; // A position in str, from 0 to str.length()-1.
		prevCh = '.'; // Prime the loop with any non-letter character.

		for (i = 0; i < str.length(); i++) {
			ch = str.charAt(i);
			if (Character.isLetter(ch) && !Character.isLetter(prevCh))
				s.append(Character.toUpperCase(ch));
			else
				s.append(ch);
			prevCh = ch; // prevCh for next iteration is ch.
		}

		return s.toString();
	}

	public static String removeChar(String s, char c) {
		String r = "";
		for (int i = 0; i < s.length(); i++) {
			if (s.charAt(i) != c)
				r += s.charAt(i);
		}
		return r;
	}

	public static String formatString(String s) {
		// remove bad characters
		s = removeChar(s, '?');
		s = removeChar(s, '!');
		s = removeChar(s, '\'');
		s = removeChar(s, '\\');
		s = removeChar(s, '/');
		s = removeChar(s, '*');
		s = removeChar(s, '$');
		s = removeChar(s, '£');
		s = removeChar(s, '^');
		s = removeChar(s, '@');
		s = removeChar(s, '"');
		s = removeChar(s, '#');
		s = removeChar(s, '~');
		s = removeChar(s, ';');
		s = removeChar(s, ':');
		s = removeChar(s, ',');
		s = removeChar(s, '§');
		s = removeChar(s, '=');
		s = removeChar(s, '+');

		// del whitespace at the begin/end
		s = s.trim();

		// replace bad characters
		s = s.replace("%", "Percent");
		s = s.replace("&", "And");
		s = s.replace("Å“", "oe");
		s = s.replace(' ', '_');
		s = s.replace('\'', '_');

		StringBuffer original = new StringBuffer("àçéèêëîïñôöùüûµÂÇéÈÊËÎÏÔÖÙÜ");
		StringBuffer replaced = new StringBuffer("aceeeeiinoouuuuACEEEEIIOOUU");

		for (int i = 0; i < original.length(); i++) {
			s = s.replace(original.charAt(i), replaced.charAt(i));
			// System.out.println("in the for to replace " + original.charAt(i)
			// + " by " + replaced.charAt(i));
		}

		return s;
	}

	/**
	 * replacePosition() replace in a String, between start and end, the current
	 * subsequences by the destination received in argument. If destination <
	 * end - start, then its completed by spaces.
	 * 
	 * @param s
	 *            the original string
	 * @param destination
	 *            the string to place in s between start and end
	 * @param start
	 *            the place where subtitute begins
	 * @param end
	 *            the place where subtitute ends
	 * @return the substitued string
	 */
	public static String replacePosition(String s, String destination,
			int start, int end, int patternLength) {
		if (start < 0 || start > s.length() || end < 0 || s == null) {
			System.out.println("je retourne null : start = " + start
					+ " ; end = " + end + " ; s.length() = " + s.length());
			return null;
		}

		StringBuffer line = new StringBuffer(s);

		// copy what is before the field to replace
		for (int i = 0; i < start; i++)
			line.setCharAt(i, s.charAt(i));

		// replace the field by destination
		for (int i = start; (i < start + destination.length()) && (i < end); i++) {
			if (i >= line.length())
				line.insert(i, destination.charAt(i - start));
			else
				line.setCharAt(i, destination.charAt(i - start));
		}

		// System.out.println("DEBUG before spaces: " + new String(line));
		// put spaces between the destination and end, in case destination
		// doesn't fill the allowed space completely
		for (int i = start + destination.length(); i < end; i++) {
			if (line.length() <= i)
				line.insert(i, ' ');
			else
				line.setCharAt(i, ' ');
		}

		// if the pattern field is larger than end - start, then we have to
		// delete it
		boolean deleted = false;
		// System.out.println("before the if, for " + destination
		// + " with end - start = " + (end - start));
		// System.out.println("DEBUG before delete: " + new String(line));
		if (patternLength > end - start) {
			line = line.delete(end, start + patternLength);
			deleted = true;
		}

		// System.out.println("DEBUG after delete + " + deleted + " : "
		// + new String(line));

		// copy the rest of the line
		if (deleted) {
			for (int i = start + patternLength; i < s.length(); i++)
				line.setCharAt(i + end - patternLength - start, s.charAt(i));
		} else {
			for (int i = end; i < s.length(); i++)
				line.setCharAt(i, s.charAt(i));
		}

		// System.out.println("DEBUG after copy: " + new String(line));
		// System.out.println("DEBUG after copy: line.length() " +
		// line.length());
		return new String(line);
	}

	public static String getFirstLine(String s) {
		if (s == null)
			return s;
		for (int i = 0; i < s.length(); i++) {
			if (s.charAt(i) == '\n')
				return s.substring(0, i);
		}
		return s;
	}

}
